package com.appkubes.mall.admin.application.order.order.dao;

import com.appkubes.mall.admin.application.order.order.entity.model.*;
import com.appkubes.mall.core.pojo.MallOrderMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface OrderDao extends MallOrderMapper {

    @Delete({"<script>",
            "Delete from mall_order where order_key in ",
            "<foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'> #{id} </foreach> ",
            "</script>"})
    int delete(@Param("ids") String[] ids);

    @Select({"select * from mall_order where order_key=#{id}"})
    OrderResult findOne(@Param("id") String id);


    @Select({"<script>",
            "SELECT " +
                    "<choose><when test='isTotal!=true'>" +
                    " o.*, u.img as user_img, u.name as user_name,u.tel as user_tel " +
                    "</when>" +
                    "<otherwise>" +
                    " count(`o`.order_key) as total" +
                    "</otherwise></choose>" +
                    " FROM `mall_order` o " +
                    " LEFT JOIN mall_user u ON u.user_key=o.user_key " +
                    " WHERE ${queryStr} " +
                    " ORDER BY o.create_time DESC " +
                    "<when test='isTotal!=true'>" +
                    "LIMIT #{page},#{limit}" +
                    "</when>",
            "</script>"})
    List<GetListOrderResult> getList(@Param("queryStr") String queryStr,
                                     @Param("page") int page,
                                     @Param("limit") int limit,
                                     @Param("isTotal") boolean isTotal
    );

    @Update({"<script>",
            "update mall_order set status='sending' where order_key in ",
            "<foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'> #{id} </foreach> ",
            "</script>"})
    int updateOrders(@Param("ids") List<String> ids);

    @Update({"<script>",
            "update mall_order set send_orderno=#{orderNo},send_kd_code=#{kdCode} where order_key=#{id}",
            "</script>"})
    int updateOrderKdInfo(@Param("id") Integer id, @Param("orderNo") String orderNo, @Param("kdCode") String kdCode);

    @Select({"<script>",
            "SELECT user_key FROM mall_order WHERE status NOT IN ('unPay','cancel') AND app_key=#{appKey} GROUP BY user_key",
            "</script>"})
    List<String> getPayUserIds(@Param("appKey") String appKey);

    @Select({"<script>",
            "SELECT sum(pay_amount) as total_amount, user_key FROM mall_order WHERE" +
                    " status NOT IN ('unPay','cancel') AND app_key=#{appKey} GROUP BY user_key ORDER BY total_amount DESC LIMIT 6",
            "</script>"})
    List<UserConsume> getUserConsumeRank(@Param("appKey") String appKey);

    @Select({"<script>",
            "SELECT sum(pay_amount) as total_amount FROM mall_order WHERE" +
                    " status NOT IN ('unPay','payFail', 'cancel') AND create_time>=${createTime}",
            "</script>"})
    UserConsume sumPayAmount(@Param("createTime") Integer createTime);

    @Select({"<script>",
            "select DATE_FORMAT(d,'%m-%d') as days,sum(t1.goods_num) as goods_count,sum(total_price) as amount_sum" +
                    " from (" +
                    " select t1.d,coalesce(1)as goods_num,coalesce(t2.amount,0)as total_price" +
                    " from(" +
                    " select DATE_SUB(curdate(),INTERVAL 0 DAY) as d" +
                    " union all" +
                    " select DATE_SUB(curdate(),INTERVAL 1 DAY) as d" +
                    " union all" +
                    " select DATE_SUB(curdate(),INTERVAL 2 DAY) as d" +
                    " union all" +
                    " select DATE_SUB(curdate(),INTERVAL 3 DAY) as d" +
                    " union all" +
                    " select DATE_SUB(curdate(),INTERVAL 4 DAY) as d" +
                    " union all" +
                    " select DATE_SUB(curdate(),INTERVAL 5 DAY) as d" +
                    " union all" +
                    " select DATE_SUB(curdate(),INTERVAL 6 DAY) as d" +
                    " )t1" +
                    " left join mall_order t2 on FROM_UNIXTIME(t2.pay_time,'%y-%m-%d') = t1.d and t2.status not in ('unPay','cancel') and t2.app_key=#{appKey}" +
                    " )t1" +
                    " GROUP BY DATE_FORMAT(d,'%m-%d')" +
                    " order by DATE_FORMAT(d,'%m-%d')",
            "</script>"})
    List<SaleDataForWeek> getSaleDataForWeek(@Param("appKey") String appKey);

    @Select({"<script>",
            "SELECT status,count(order_key) as record_num FROM mall_order WHERE app_key=#{appKey} GROUP BY status",
            "</script>"})
    List<OrderSum> getOrderSum(@Param("appKey") String appKey);
}
